package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.function.common.GetRest;
import com.function.common.RestResponse;
import com.sxbbc.common.basics.exceptions.ThrowJsonException;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.BBusiness;
import com.sxbbc.common.core.entity.jz.CCollectBus;
import com.sxbbc.common.core.entity.jz.CCustomer;
import com.sxbbc.common.core.mapper.jz.CCollectBusMapper;
import com.sxbbc.common.core.service.BBusinessService;
import com.sxbbc.common.core.service.ICCollectBusService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.service.ICCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 收藏店铺 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-05-16
 */
@Service
public class CCollectBusServiceImpl extends ServiceImpl<CCollectBusMapper, CCollectBus> implements ICCollectBusService {

    @Autowired
    private ICCustomerService customerService;
    @Autowired
    private BBusinessService bBusinessService;

    @Override
    public RestResponse handleCollectBus(CCollectBus collectBus) {
        boolean handle = false;
        CCustomer cCustomer = customerService.getById(collectBus.getCid());
        if (cCustomer == null) {
            return GetRest.getFail("获取用户信息失败");
        }
        BBusiness business = bBusinessService.getById(collectBus.getBid());
        if (business == null) {
            return GetRest.getFail("该店铺不存在");
        }
        QueryWrapper<CCollectBus> wrapper = new QueryWrapper<>();
        wrapper.eq("cid",collectBus.getCid()).eq("bid",collectBus.getBid());
        CCollectBus cCollectBus = super.getOne(wrapper);
        if (cCollectBus == null) {
            collectBus.setBurl(business.getHeadUrl());
            handle = super.save(collectBus);
            if (!handle) {
                throw new ThrowJsonException("收藏失败");
            }
            return GetRest.getSuccess("收藏成功");
        }
        switch (collectBus.getCollectFlag()){
            case 1:
                cCollectBus.setStatus(StaticUtils.STATUS_NO);
                handle = super.updateById(cCollectBus);
                break;
            case 0:
                cCollectBus.setStatus(StaticUtils.STATUS_YES);
                handle = super.updateById(cCollectBus);
                break;
            default:
                break;
        }
        if (!handle) {
            throw new ThrowJsonException("处理收藏失败");
        }
        return GetRest.getSuccess("处理成功");
    }
}
